//
//  ClubFindGameView.swift
//  77Club
//
//  Created by Apple on 2024/8/27.
//

import UIKit
import FSPagerView
import Cosmos

//MARK: 找游戏左侧分类cell
class ClubFindGameLeftCell: UITableViewCell {
    
    private let shadowView = c_make_ex(UIView.self).end()
    private let selImageV = c_make_ex(UIImageView.self)
        .mode(.scaleAspectFill).image("Home_findGame_sel").end()
    private let titleLabel = c_make_ex(UILabel.self)
        .color("#7B7D80").font(14.font_medium).end()
    
    var model: ClubHomeNormalGameTypeModel? {
        didSet{
            guard let md = model else { return }
            
            titleLabel.text = md.name
            if md.isSel {
                shadowView.isHidden = false
                selImageV.isHidden = false
                titleLabel.textColor = "#FF7500".toColor
                titleLabel.leadingConstraint?.constant = 26.0.Scale
            }else{
                shadowView.isHidden = true
                selImageV.isHidden = true
                titleLabel.textColor = "#7B7D80".toColor
                titleLabel.leadingConstraint?.constant = 12.0.Scale
            }
        }
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        selectionStyle = .none
        backgroundColor = .clear
        contentView.backgroundColor = .clear
        
        contentView.subviews{
            shadowView
            selImageV
            titleLabel
        }

        shadowView.isHidden = true
        selImageV.isHidden = true
        
        shadowView.fillContainer()
        selImageV.leading(12.0.Scale).size(12.0.Scale).centerVertically()
        titleLabel.leading(12.0.Scale).trailing(5.0.Scale).centerVertically()
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        shadowView.rae.gradientColors(.rightToLeft(["#F27810".toColor.alpha(0),"#F27810".toColor.alpha(0.12)]))
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

//MARK: 找游戏Bannercell
class ClubFindGameBannerCell: UITableViewCell,FSPagerViewDelegate,FSPagerViewDataSource {
    
    private var banner = FSPagerView()
    
    var models: [ClubClassifyGameSlideModel]? {
        didSet{
            banner.reloadData()
        }
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        selectionStyle = .none
        backgroundColor = .clear
        contentView.backgroundColor = .clear
        
        contentView.subviews{
            banner
        }
        
        banner.dataSource = self
        banner.delegate = self
        banner.isInfinite = true
        banner.register(ClubBannerPagerCell.self, forCellWithReuseIdentifier: "ClubBannerPagerCell")
        banner.automaticSlidingInterval = 3
        banner.removesInfiniteLoopForSingleItem = true
//        banner.itemSize = CGSize(width: .screenWidth, height: 200.0.Scale)
//        banner.backgroundColor = .clear
        
        banner.top(0).bottom(0).leading(0).trailing(0).height(200.0.Scale)
    }
    
    func numberOfItems(in pagerView: FSPagerView) -> Int {
        return models?.count ?? 0
    }
    
    func pagerView(_ pagerView: FSPagerView, cellForItemAt index: Int) -> FSPagerViewCell {
        let cell = pagerView.dequeueReusableCell(withReuseIdentifier: "ClubBannerPagerCell", at: index) as! ClubBannerPagerCell
        cell.model = self.models?[index]
        return cell
    }
    
    func pagerView(_ pagerView: FSPagerView, didSelectItemAt index: Int) {
        
        let gameId = self.models?[index].gid ?? ""
        let detailVC = ClubGameDetailViewController()
        detailVC.gameId = gameId
        UIApplication.topController()?.navigationController?.pushViewController(detailVC, animated: true)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

//MARK: 找游戏Bannercell
class ClubBannerPagerCell:FSPagerViewCell {
    
    private let bgView = c_make_ex(UIView.self)
        .corner(12.0.Scale).bgColor(.white).end()
    private let topImageview = c_make_ex(UIImageView.self)
        .mode(.scaleAspectFill).end()
    private let iconImageview = c_make_ex(UIImageView.self)
        .mode(.scaleAspectFill).corner(8.0.Scale).end()
    private let bgShadowView = c_make_ex(UIView.self)
        .bgColor(.black.alpha(0.1)).end()
    private let iconShadowView = c_make_ex(UIView.self)
        .bgColor(.black.alpha(0.1)).end()
    private let infoLabel = c_make_ex(UILabel.self)
        .color("#47494D").font(12.font_semiBold)
        .text("详情").corner(11).alignment(.center)
        .border("#47494D", width: 1).end()
    private let nameLabel = c_make_ex(UILabel.self)
        .color("#2E3033").font(12.font_semiBold).end()
    private let attentionLabel = c_make_ex(UILabel.self)
        .color("#E8E9EB").font(9.font_semiBold)
        .bgColor(.black.alpha(0.5)).corner(9.0.Scale).end()
    private let scoreLabel = UIIconButton(style: .leading(itemSpace: 1))
    
    var model: ClubClassifyGameSlideModel? {
        didSet{
            guard let md = model else { return }
            
            topImageview.loadImage(url: md.image)
            iconImageview.loadImage(url: md.icon)
            nameLabel.text = md.game_name
            scoreLabel.setTitle(md.point, for: .normal)
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        contentView.layer.shadowColor = UIColor.clear.cgColor
        
        contentView.subviews{
            bgView.subviews{
                topImageview.subviews{
                    bgShadowView
                }
                iconImageview.subviews{
                    iconShadowView
                }
                infoLabel
                nameLabel
                attentionLabel
                scoreLabel
            }
        }
        
        scoreLabel.style { v in
            v.iconSize = CGSize(width: 16.0.Scale, height: 16.0.Scale)
            v.setImage("Home_star".toImage, for: .normal)
            v.setTitleColor("#FF7600".toColor, for: .normal)
            v.titleLabel?.font = 12.font_medium
            v.isUserInteractionEnabled = false
        }
//        bgView.layer.masksToBounds = true
        bgView.top(0).bottom(5.0.Scale).leading(12.0.Scale).trailing(12.0.Scale).height(194.0.Scale)
        topImageview.top(0).leading(0).trailing(0).height(135.0.Scale)
        bgShadowView.fillContainer()
        iconImageview.leading(16.0.Scale).size(36.0.Scale)
        iconImageview.Top == topImageview.Bottom + 12.0.Scale
        iconShadowView.fillContainer()
        infoLabel.trailing(12.0.Scale).width(48.0.Scale).height(22.0.Scale)
        infoLabel.CenterY == iconImageview.CenterY
        nameLabel.leading(64.0.Scale).trailing(75.0.Scale)
        nameLabel.Top == topImageview.Bottom + 13.0.Scale
        scoreLabel.leading(64.0.Scale).height(16.0.Scale)
        scoreLabel.Top == nameLabel.Bottom + 3.0.Scale
        
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

//MARK: 找游戏精选cell
class ClubFindGameHandpickCell: UITableViewCell {
    
    private let bgView = c_make_ex(UIView.self)
        .corner(12.0.Scale).bgColor(.white).end()
    private let topImageview = c_make_ex(UIImageView.self)
        .mode(.scaleAspectFill).end()
    private let iconImageview = c_make_ex(UIImageView.self)
        .mode(.scaleAspectFill).corner(8.0.Scale).end()
    private let bgShadowView = c_make_ex(UIView.self)
        .bgColor(.black.alpha(0.1)).end()
    private let iconShadowView = c_make_ex(UIView.self)
        .bgColor(.black.alpha(0.1)).end()
    private let numLabel = c_make_ex(UILabel.self)
        .color(.white).font(9.font_semiBold)
        .bgColor(.black.alpha(0.5)).corner(9).alignment(.center)
        .end()
    private let infoLabel = c_make_ex(UILabel.self)
        .color("#47494D").font(12.font_semiBold)
        .text("详情").corner(11).alignment(.center)
        .border("#47494D", width: 1).end()
    private let nameLabel = c_make_ex(UILabel.self)
        .color("#2E3033").font(12.font_semiBold).end()
    private let attentionLabel = c_make_ex(UILabel.self)
        .color("#E8E9EB").font(9.font_semiBold)
        .bgColor(.black.alpha(0.5)).corner(9.0.Scale).end()
    private var starView = CosmosView()
    private let scoreLabel = c_make_ex(UILabel.self)
        .color("#FF7500").font(14.font_wide).end()
    
    var model: ClubClassifyGameModel? {
        didSet{
            guard let md = model else { return }
            
            topImageview.loadImage(url: md.image)
            iconImageview.loadImage(url: md.icon)
            nameLabel.text = md.name
            starView.rating = (Double(md.point) ?? 0) / 2.0
            scoreLabel.text = (md.point)
            numLabel.text = "  " + md.number + "  "
        }
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        selectionStyle = .none
        backgroundColor = .clear
        contentView.backgroundColor = .clear
        
        contentView.subviews{
            bgView.subviews{
                topImageview.subviews{
                    bgShadowView
                }
                iconImageview.subviews{
                    iconShadowView
                }
                numLabel
                infoLabel
                nameLabel
                attentionLabel
                starView
                scoreLabel
            }
        }
        
        starView.rating = 0
        starView.settings.starSize = 12.0.Scale
        starView.settings.starMargin = 2.0.Scale
        starView.settings.fillMode = .half
        starView.settings.emptyImage = "Common_emptyStar".toImage
        starView.settings.filledImage = "Common_fillStar".toImage
        starView.isUserInteractionEnabled = false
        
        bgView.top(11.0.Scale).bottom(5.0.Scale).leading(12.0.Scale).trailing(12.0.Scale).height(194.0.Scale)
        topImageview.top(0).leading(0).trailing(0).height(135.0.Scale)
        bgShadowView.fillContainer()
        iconImageview.leading(16.0.Scale).size(36.0.Scale)
        iconImageview.Top == topImageview.Bottom + 12.0.Scale
        iconShadowView.fillContainer()
        infoLabel.trailing(12.0.Scale).width(48.0.Scale).height(22.0.Scale)
        infoLabel.CenterY == iconImageview.CenterY
        nameLabel.leading(64.0.Scale).trailing(75.0.Scale)
        nameLabel.Top == topImageview.Bottom + 13.0.Scale
        starView.leading(64.0.Scale).height(12.0.Scale).width(68.0.Scale)
        starView.Top == nameLabel.Bottom + 3.0.Scale
        scoreLabel.CenterY == starView.CenterY
        scoreLabel.Leading == starView.Trailing + 4.0.Scale
        numLabel.top(12.0.Scale).trailing(12.0.Scale).height(18.0.Scale)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

//MARK: 找游戏cell
class ClubFindNormalCell: UITableViewCell {
    
    private let iconImageV = c_make_ex(UIImageView.self)
        .corner(12.0.Scale).mode(.scaleAspectFill).end()
    private let shadowView = c_make_ex(UIView.self)
        .bgColor(.black.alpha(0.1)).end()
    private let nameLabel = c_make_ex(UILabel.self)
        .color(.black).font(14.font_semiBold).end()
    private let tagLabel = c_make_ex(UILabel.self)
        .bgColor("#E55C67".toColor.alpha(0.1)).font(8.font_medium)
        .corner(4.0.Scale).color("#E55C67").end()
    private let subLabel = c_make_ex(UILabel.self)
        .color("#AEB0B3").font(13.font_semiBold).end()
    private var starView = CosmosView()
    private let scoreLabel = c_make_ex(UILabel.self)
        .color("#FF7500").font(14.font_wide).end()
    
    var model: ClubClassifyGameModel? {
        didSet{
            guard let md = model else { return }
            
            iconImageV.loadImage(url: md.icon)
            nameLabel.text = md.name
            starView.rating = (Double(md.point) ?? 0) / 2.0
            scoreLabel.text = (md.point)
            if md.pay.count > 0 {
                tagLabel.isHidden = false
                tagLabel.text = " " + md.pay + " "
            }else{
                tagLabel.isHidden = true
            }
            let tagStr = md.biaoqian.joined(separator: " · ")
            subLabel.text = tagStr
        }
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        selectionStyle = .none
        backgroundColor = .clear
        contentView.backgroundColor = .clear
        
        contentView.subviews{
            iconImageV.subviews{
                shadowView
            }
            nameLabel
            tagLabel
            subLabel
            starView
            scoreLabel
        }

        
        starView.rating = 0
        starView.settings.starSize = 12.0.Scale
        starView.settings.starMargin = 2.0.Scale
        starView.settings.fillMode = .half
        starView.settings.emptyImage = "Common_emptyStar".toImage
        starView.settings.filledImage = "Common_fillStar".toImage
        starView.isUserInteractionEnabled = false
        
        iconImageV.leading(12.0.Scale).size(60.0.Scale).top(12.0.Scale).bottom(12.0.Scale)
        shadowView.fillContainer()
        nameLabel.leading(84.0.Scale).top(12.0.Scale).width(<=140)
        tagLabel.height(15.0.Scale)
        tagLabel.Leading == nameLabel.Trailing + 3.0.Scale
        tagLabel.CenterY == nameLabel.CenterY
        subLabel.leading(84.0.Scale).trailing(20.0.Scale)
        subLabel.Top == nameLabel.Bottom + 3.0.Scale
        starView.leading(84.0.Scale).height(12.0.Scale).width(68.0.Scale)
        starView.Bottom == iconImageV.Bottom - 3.0.Scale
        scoreLabel.CenterY == starView.CenterY
        scoreLabel.Leading == starView.Trailing + 4.0.Scale
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}


//MARK: 找游戏空白cell
class ClubFindEmptyCell: UITableViewCell {
    
    private let iconImageView = c_make_ex(UIImageView.self)
        .mode(.scaleAspectFill).image("Common_empty").end()
    private let titleLabel = c_make_ex(UILabel.self)
        .color("#2E3033").font(14.font_semiBold).alignment(.center)
        .text("没有找到游戏哦~").end()
    
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        selectionStyle = .none
        backgroundColor = .clear
        contentView.backgroundColor = .clear
        
        contentView.subviews{
            iconImageView
            titleLabel
        }

        
        iconImageView.top(180.0.Scale).width(197.0.Scale).height(150.0.Scale).centerHorizontally()
        titleLabel.leading(20).trailing(20).bottom(150.0.Scale)
        titleLabel.Top == iconImageView.Bottom + 24.0.Scale
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

//MARK: 分区titileView
class ClubFindGameHeaderView: UITableViewHeaderFooterView {
         
    private let nameLabel = c_make_ex(UILabel.self)
        .color("#17181A").font(14.font_semiBold).end()
    
    var titleString: String? {
        didSet{
            guard let str = titleString else { return }
            nameLabel.text = str
        }
    }
    
    override init(reuseIdentifier: String?) {
        super.init(reuseIdentifier: reuseIdentifier)
        backgroundColor = "#F7F8FA".toColor
        contentView.backgroundColor = "#F7F8FA".toColor
        
        addSubview(nameLabel)
        
        nameLabel.snp.makeConstraints { make in
            make.leading.equalToSuperview().offset(24.0.Scale)
            make.top.equalToSuperview().offset(8.0.Scale)
        }
    }
        
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override func layoutSubviews() {
        super.layoutSubviews()
    }
    
}
